package com.xnfh.home.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.RadioButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.ProductIntroduce
import com.xnfh.framework.bean.ProductIntroduceItem
import com.xnfh.home.R
import com.xnfh.home.adapter.ProductIntroduceAdapter
import com.xnfh.home.databinding.ActivityProductIntroduceBinding
import com.xnfh.home.viewmodel.ProductIntroduceViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/8 15:50
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/8 15:50
 * @UpdateRemark: 更新说明
 */
class ProductIntroduceActivity : BaseActivity<ActivityProductIntroduceBinding>() {
    companion object{
        fun start(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, ProductIntroduceActivity::class.java)
            if (bundle != null) {
                intent.putExtra("bundle", bundle)
            }
            context.startActivity(intent)
        }
    }
    override fun getLayoutID(): Int = R.layout.activity_product_introduce
    private lateinit var adapter: ProductIntroduceAdapter
    private var page = 1
    private var productType: String? = null
    private lateinit var viewModel: ProductIntroduceViewModel
    override fun init() {
        setHeaderTitle(getString(R.string.str_product))
        mBind.productIntroductRecycle.layoutManager = GridLayoutManager(this, 2)
        adapter = ProductIntroduceAdapter()
        mBind.productIntroductRecycle.adapter = adapter
        mBind.productIntroductSmart.autoRefresh()
        mBind.productIntroductSmart.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.requestProductList(
                    page,
                    mBind.productSchemeEdit.text.toString(),
                    productType
                )
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.requestProductList(
                    page,
                    mBind.productSchemeEdit.text.toString(),
                    productType
                )
            }
        })

        adapter.setOnItemClickListener { adapter, view, position ->
            val bean = (adapter.data[position] as ProductIntroduceItem)
            val bundle = Bundle()
            bundle.putSerializable("ProductDetail", bean)
            ProductDetailActivity.start(this,bundle)
        }

        //选中的类型
        mBind.productIntorductRadioGroup.setOnCheckedChangeListener { group, checkedId ->
            //1==全部，全部就不传这个参数，否则就传入  1.基施，2.追肥，3.叶喷，4.其他
            val checkedRadioButtonId = group.checkedRadioButtonId
            val radioButton = findViewById<RadioButton>(checkedRadioButtonId)
            when (radioButton.text.toString()) {
                resources.getString(R.string.str_all) -> productType = null
                resources.getString(R.string.str_product_jishi) -> productType = "1"
                resources.getString(R.string.str_product_zf) -> productType = "2"
                resources.getString(R.string.str_product_yp) -> productType = "3"
                resources.getString(R.string.str_other) -> productType = "4"
            }
            mBind.productIntroductSmart.autoRefresh()
        }

        //搜索框 搜索触发
        mBind.productSchemeEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.productIntroductSmart.autoRefresh()
            }
            val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
            return@setOnEditorActionListener false
        }

        mBind.productSchemeEdit.setOnDeleteClickListener {
            mBind.productIntroductSmart.autoRefresh()
        }

    }

    override fun observe() {
        viewModel = ProductIntroduceViewModel()
        viewModel.productList.observe(this,object :BaseStateObserver<ProductIntroduce>(){
            override fun getRespDataSuccess(it: ProductIntroduce) {
                super.getRespDataSuccess(it)
                mBind.productIntroductSmart.let {
                    it.finishLoadMore()
                    it.finishRefresh()
                }
                if(it.list.isEmpty()){
                    adapter.setNewInstance(null)
                    adapter.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter.setNewInstance(it.list.toMutableList())
                }else{
                    adapter.addData(it.list)
                }
            }

            override fun getRespDataEnd() {
                super.getRespDataEnd()
                mBind.productIntroductSmart.let {
                    it.finishLoadMore()
                    it.finishRefresh()
                }
            }
        })
    }
}